package logic

import (
	"context"
	"three-sxp/models/es"

	"three-sxp/goodsrpc/goodsrpc"
	"three-sxp/goodsrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SearchEsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSearchEsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchEsLogic {
	return &SearchEsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// SearchEs todo: 商品分词搜索
func (l *SearchEsLogic) SearchEs(in *goodsrpc.SearchEsRequest) (*goodsrpc.SearchEsResponse, error) {
	// todo: add your logic here and delete this line
	GoodsAll, err := es.SearchEs("goods", "GoodsName", in.Title)
	if err != nil {
		return &goodsrpc.SearchEsResponse{
			Code: 400,
			Msg:  err.Error(),
		}, nil
	}
	var Goods []*goodsrpc.Goods
	for _, v := range GoodsAll {
		var goods goodsrpc.Goods
		goods.Id = int64(v.ID)
		goods.GoodsName = v.GoodsName
		goods.GoodsPrice = float32(v.GoodsPrice)
		goods.GoodsStock = int64(v.GoodsStock)
		goods.GoodsDetails = v.GoodsDetails
		Goods = append(Goods, &goods)
	}
	return &goodsrpc.SearchEsResponse{
		Code: 200,
		Msg:  "搜索成功",
		Data: Goods,
	}, nil
}
